package cn.kxx.controller;


import cn.kxx.pojo.Product;
import cn.kxx.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: yanqi
 * @Date: 14:34
 * @Desc: 产品操作
 */
@Controller
@RequestMapping("product")
public class ProductController {


    //注入业务层
    @Autowired
    private ProductService productService;

    /**
     * 指定删除
     * @param id
     * @return
     */
   /* @RequestMapping("deleteById")
    public String deleteById(@RequestParam("id") Integer id){
        this.productService.deleteById(id);
        return "redirect:findAll.do";
    }
*/

    /**
     * 新增产品
     * @param product
     * @return
     */
    @RequestMapping("save")
    public String save(Product product){
        this.productService.save(product);
        return "redirect:findAll.do";
    }


    /**
     * 查询所有产品
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "size",defaultValue = "5") Integer size){

        if (page<=0){
            page=1;
        }

        PageHelper.startPage(page,size);

        List<Product> productList =  this.productService.findAll();

        PageInfo pageInfo = new PageInfo(productList);

        int lastPage = pageInfo.getLastPage();
        int pageNum = pageInfo.getPageNum();
        if (pageNum>=lastPage){

            pageInfo.setPageNum(lastPage);
            PageHelper.startPage(pageInfo.getPageNum(),size);

            List<Product> productList1 =  this.productService.findAll();
            PageInfo pageInfo1 = new PageInfo(productList1);

            ModelAndView mv1 = new ModelAndView();
            mv1.addObject("pageInfo",pageInfo1);
            mv1.setViewName("product-list");
            return mv1;
        }


        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("product-list");
        return mv;
    }
    
    /**
     *@描述: 删除指定产品
     *@参数: id
     *@返回值: 
     *@创建人: kxx
     *@创建时间: 2022/10/31 20:58
     *@修改人和其它信息:
     */

    @RequestMapping("deleteById")
    public String delectById(@RequestParam("id") Integer id){

        productService.delectById(id);

        return "forward:/product/findAll.do";
    }


    /**
     *@描述: 选中删除
     *@参数:
     *@返回值:
     *@创建人: kxx
     *@创建时间: 2022/10/31 22:26
     *@修改人和其它信息:
     */
    @RequestMapping("delselectedservlet")
    public String delselectedservlet(HttpServletRequest request ) throws UnsupportedEncodingException {

        request.setCharacterEncoding("utf-8");

        String ids = request.getParameter("ids");
        String[] split = ids.split(",");

        productService.delectAll(split);

        return "forward:/product/findAll.do";
    }

    /**
     *@描述: 展示产品详情
     *@参数:  产品 id
     *@返回值: pruduct对象
     *@创建人: kxx
     *@创建时间: 2022/11/6 16:21
     *@修改人和其它信息: 暂无
     */

    @RequestMapping("findById")
public  ModelAndView findById(Integer id){

        ModelAndView mv = new ModelAndView();

        Product product = productService.findById(id);
        mv.setViewName("product-show");
        mv.addObject("product",product);
        return mv;
    }





}











